﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AG.Data;
using AG.MapEditor.Data;

namespace AGMapEditor.Controls
{
    public partial class AGSelectTerrainPanel : UserControl
    {
        private AGEMapPanel _ctlMapPanel;

        public AGSelectTerrainPanel(AGEMapPanel mapPanel)
        {
            InitializeComponent();

            BindTerrainTree();

            _ctlMapPanel = mapPanel;
        }

        private void BindTerrainTree()
        {
            _ctlTreeTerrain.Nodes.Clear();
            for (int index = 0; index < AGMEContext.Current.Data.TerrainList.Count; index++)
            {
                AGTerrain terrain = AGMEContext.Current.Data.TerrainList[index];
                if (terrain.IsSimple())
                {
                    TreeNode node = new TreeNode();
                    node.Text = terrain.Caption;
                    node.Tag = terrain;
                    _ctlTreeTerrain.Nodes.Add(node);

                    for (int tileIndex = 0; tileIndex < terrain.Tiles.Count; tileIndex++)
                    {
                        AGTerrainTile tile = terrain.Tiles[tileIndex];
                        TreeNode nodeTile = new TreeNode();
                        nodeTile.Text = tile.ToString();
                        nodeTile.Tag = tile;

                        node.Nodes.Add(nodeTile);
                    }
                }
            }
        }

        private void _ctlTreeTerrain_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Parent != null)
            {
                AGTerrain terrain = e.Node.Parent.Tag as AGTerrain;
                AGTerrainTile tile = e.Node.Tag as AGTerrainTile;
                _ctlMapPanel.SetSelectTerrain(terrain, tile);
            }
        }
    }
}
